package com.fenrirtec.aepp.common.api;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fenrirtec.aepp.common.dto.MemberDto;
import com.fenrirtec.aepp.common.service.TenderService;
import com.fenrirtec.aepp.common.service.impl.TenderServiceImpl;
import com.fenrirtec.aepp.common.support.JSONActionSupport;
import com.fenrirtec.core.definition.BaseDefinition.API;
import com.fenrirtec.core.definition.BaseDefinition.API.Common;
import com.fenrirtec.core.exception.DatabaseException;
import com.fenrirtec.core.exception.UnexpectedException;
import com.fenrirtec.core.utils.JsonUtils;

public class TenderMemberSearchAPI extends JSONActionSupport {

	private static final long serialVersionUID = 1L;

	private static final Logger logger = LoggerFactory.getLogger(TenderMemberSearchAPI.class);

	public TenderMemberSearchAPI() {
	}
	
	private String param;

	public String getParam() {
		return this.param;
	}

	public void setParam(String param) {
		this.param = param;
	}

    @Override
	protected List<ErrorInfo> doValidate() {
		return null;
	}

	@Override
	protected JSONObject doExecute() {
		
		TenderService service = new TenderServiceImpl();
		List<MemberDto> enrollMemberList = null;
        List<MemberDto> finalistMemberList = null;
        List<MemberDto> quoteMemberList = null;
        MemberDto bidMember = null;
        Boolean result = false;
		
		try {
			JSONObject jsonObj = JSONObject.parseObject(this.param);
			Integer tenderId = jsonObj.getInteger("tender_id");
			enrollMemberList = service.findEnrollMember(tenderId);
			finalistMemberList = service.findFinalistMember(tenderId);
            quoteMemberList = service.findQuoteMember(tenderId);
            bidMember = service.findBidMember(tenderId);
            result = true;
		} catch (DatabaseException e) {
			if (logger.isErrorEnabled()) {
				logger.error("database error occurred.", e);
			}
			throw new JSONActionStatusException(API.Common.Status.DB_ERROR, e);
		} catch (UnexpectedException e) {
			if (logger.isErrorEnabled()) {
				logger.error("unexpected error occurred.", e);
			}
			throw new JSONActionStatusException(API.Common.Status.DB_ERROR, e);
		}
		
		JSONObject root = new JSONObject();
		root.put(Common.Response.RESULT, result);
		if (enrollMemberList != null && enrollMemberList.size() > 0) {
		    root.put("enroll_member", JsonUtils.fromBean(enrollMemberList));
		} else {
		    root.put("enroll_member", new JSONArray());
		}
		if (finalistMemberList != null && finalistMemberList.size() > 0) {
            root.put("finalist_member", JsonUtils.fromBean(finalistMemberList));
        } else {
            root.put("finalist_member", new JSONArray());
        }
        if (quoteMemberList != null && quoteMemberList.size() > 0) {
            root.put("quote_member", JsonUtils.fromBean(quoteMemberList));
        } else {
            root.put("quote_member", new JSONArray());
        }
        if (bidMember != null) {
            root.put("bid_member", JsonUtils.fromBean(bidMember));
        } else {
            root.put("bid_member", new JSONObject());
        }
		return root;
	}

}
